package com.chum.chamber.dao;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.chum.chamber.entity.LoginEntity;
import com.chum.chamber.model.Login;

@Repository
public class LoginDAOImpl implements LoginDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	@ExceptionHandler
	public boolean validateUser(Login login) {
		boolean valid = false;
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(LoginEntity.class)
		.add(Restrictions.eq("userName", login.getUserName()))
		.add(Restrictions.eq("password", login.getPassword()));
		if (criteria.list() != null && !criteria.list().isEmpty()) {
			valid = true;
		}
		System.out.println(" User is valid :: "+valid);
		return valid;
	}
}
